home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / bplus25a.zip / LISTTREE.C < prev    next >
Text File  |  1990-12-30  |  3KB  |  96 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /*                             LISTTREE.C                                */
  4. /*                                                                       */
  5. /*  This program displays each block in an index file.  The root node    */
  6. /*  of the index tree is displayed first.  Then the left most descendant */
  7. /*  block is displayed for each level in the tree.  After the leaf block */
  8. /*  is displayed, the right most descendant from the ancestor block is   */
  9. /*  displayed.  That is, a preordering method of transversing the tree   */
  10. /*  is used.  The complete block is displayed including the data file    */
  11. /*  address, the index file address, and the index key.  The printtree   */
  12. /*  routine is recursive.                                                */
  13. /*                                                                       */
  14. /*************************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "bplus.h"
  19.  
  20. /* constant and macros */
  21. #define  NULLREC  (-1L)
  22. #define  ENT_ADR(pb,off)  ((ENTRY*)((char*)((pb)->entries) + off))
  23. #define  ENT_SIZE(pe)     strlen((pe)->key) + 1 + 2 * sizeof(RECPOS)
  24.  
  25. /* global variables defined in BPLUS.C */
  26. extern  IX_DESC    *pci;
  27. extern  BLOCK      *block_ptr;
  28.  
  29. IX_DESC ixfile;
  30.  
  31. void pascal retrieve_block(int, RECPOS);
  32. int pascal copy_entry(ENTRY *, ENTRY *);
  33. void print_blk(BLOCK *);
  34. void printtree(                           /* print the index tree */
  35.    RECPOS num,                            /* index block address */
  36.    int l,                                 /* indent each level l */
  37.    int level);                            /* level in index file */
  38.  
  39.  
  40. void print_blk(pb)                 /* list each entry in block pb */
  41.    BLOCK *pb;
  42.   {
  43.     int i;
  44.     i = 0;
  45.     printf("%ld  ",pb->brec);
  46.     printf("%d  %ld  ",pb->bend,pb->p0);
  47.     while (i < pb->bend)
  48.       {
  49.         printf(" %ld ",ENT_ADR(pb,i) -> idxptr);
  50.         printf(" %ld ",ENT_ADR(pb,i) -> recptr);
  51.         printf("%s    ",ENT_ADR(pb,i) -> key);
  52.         i = i + ENT_SIZE(ENT_ADR(pb,i));
  53.       }
  54.     printf("\n");
  55.   }
  56.  
  57. void printtree(num, l, level)             /* print the index tree */
  58.    RECPOS num;                            /* index block address */
  59.    int l;                                 /* indent each level l */
  60.    int level;                             /* level in index file */
  61.   {
  62.     long  address;
  63.     int  end, i, j;
  64.     if (num != NULLREC)
  65.       {
  66.         for (i=1; i<=l; i++)
  67.           printf("    ");
  68.         retrieve_block(level, num);
  69.         print_blk(block_ptr);
  70.         address = block_ptr -> p0;
  71.         printtree(address, l + 1,level + 1);
  72.         retrieve_block(level, num);
  73.         j = 0;
  74.         end = block_ptr -> bend;
  75.         while (j < end)
  76.         {
  77.           address = ENT_ADR(block_ptr,j) -> idxptr;
  78.           printtree(address, l + 1, level + 1);
  79.           retrieve_block(level, num);
  80.           j = j + ENT_SIZE(ENT_ADR(block_ptr,j));
  81.         }
  82.       }
  83.     }
  84.  
  85. void main()
  86.   {
  87.     char name[80];
  88.  
  89.     printf("\n\n                       Display An Index Tree\n\n");
  90.     printf("     Name of index file: ");
  91.     gets(name);
  92.     open_index(name,&ixfile, 1);
  93.     printtree(0L,1,0);
  94.     close_index(&ixfile);
  95.   }
  96.